﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using PivotWebsite.Controllers;
using System.IO;
using System.Linq;
using System.Web.Http;

namespace PivotWebsite.Tests.Controllers
{
    [TestClass]
    public class CollectionsControllerTest
    {
        [TestInitialize]
        public void TestReset()
        {
            Utils.TestInit();
        }

        [TestCleanup]
        public void TestCleanup()
        {
            Utils.TestFinalize();
        }

        [TestMethod]
        public void TestGetEmptyCollectionList()
        {
            var tc = new CollectionsController();
            Assert.AreEqual(0, tc.GetAllCollections().Count(), "number for an empty dataset");
        }

        [TestMethod]
        [ExpectedException(typeof(HttpResponseException))]
        public void TestGetNonExistantCollection()
        {
            var tc = new CollectionsController();
            var p = tc.GetCollectionById(5);
        }

        [TestMethod]
        public void TestGetCollectionByName()
        {
            var tc = new CollectionsController();
            tc.PutCollection("hi");
            var p = tc.GetCollection("hi");
            Assert.IsNotNull(p);
            Assert.AreEqual(1, p.Id);
        }

        [TestMethod]
        [ExpectedException(typeof(HttpResponseException))]
        public void TestGetCollecionByNameBad()
        {
            var tc = new CollectionsController();
            var p = tc.GetCollection("hi");
        }

        [TestMethod]
        public void PutNewCollection()
        {
            var tc = new CollectionsController();
            var p = tc.PutCollection("freak");
            Assert.AreEqual("freak", p.Name);

            var ci = tc.GetAllCollections().ToArray();
            Assert.AreEqual(p.Id, ci.Length, "Number after insert");
            Assert.AreEqual("freak", ci[0].Name, "Name of the collection");
        }

        [TestMethod]
        public void Put2NewCollection()
        {
            var tc = new CollectionsController();
            var p2 = tc.PutCollection("freak");
            var p1 = tc.PutCollection("stuffme");

            Assert.IsTrue(p1.Id != p2.Id, "Id's shouldn't be the same");

            var ci = tc.GetAllCollections().ToArray();
            Assert.AreEqual(2, ci.Length, "Number after insert");
            Assert.AreEqual("stuffme", ci[1].Name, "Name of the collection");
        }

        [TestMethod]
        [ExpectedException(typeof(HttpResponseException))]
        public void PutNullCollection()
        {
            var tc = new CollectionsController();
            var p2 = tc.PutCollection(null);
        }

        [TestMethod]
        public void PutSameCollectionTwice()
        {
            var tc = new CollectionsController();
            var p1 = tc.PutCollection("hi");
            var p2 = tc.PutCollection("hi");
            Assert.AreEqual(p1.Id, p2.Id, "shoudl be the same");
        }

        [TestMethod]
        public void DeleteCollection()
        {
            var tc = new CollectionsController();
            var p1 = tc.PutCollection("hi");
            tc.DeleteCollection(p1.Id);
            var ci = tc.GetAllCollections().ToArray();
            Assert.AreEqual(0, ci.Length, "deleted what we created...");

        }

        [TestMethod]
        public void DeleteCollectionByName()
        {
            var tc = new CollectionsController();
            var p1 = tc.PutCollection("hi");
            tc.DeleteCollection("hi");
            var ci = tc.GetAllCollections().ToArray();
            Assert.AreEqual(0, ci.Length, "deleted what we created...");
        }

        [TestMethod]
        public void DeleteCollectionKillsWebsite()
        {
            var tc = new CollectionsController();
            var p1 = tc.PutCollection("hi");
            var dir = new DirectoryInfo(Path.Combine(PivotIO.XML.Utils.GetWebDataDirectory().FullName, p1.Id.ToString()));
            dir.Create();
            tc.DeleteCollection(p1.Id);
            dir.Refresh();
            Assert.IsFalse(dir.Exists, "Directory should have been removed");
        }
    }
}
